/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bl;

import Config.Localization;
import da.DALoanType;
import entities.LoanType;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author foly
 */
public class BLLoanType {

    DALoanType daLoanType;

    public BLLoanType() {
        daLoanType = new DALoanType();
    }

     public ArrayList<LoanType> getEnable() throws Exception {
        return daLoanType.getEnable();
    }
    //get all loan type
    public ArrayList<LoanType> getAll() throws Exception {
        return daLoanType.getAll();
    }

    public ArrayList<LoanType> getByID(String sid) throws Exception {
        int id = 0;
        boolean check = false;
        try {
            id = Integer.parseInt(sid);
            check = true;
        } catch (Exception ex) {
            check = false;
            //ex.printStackTrace();
        }
        if (check) {
            return daLoanType.getByID(id);
        } else {
            JOptionPane.showMessageDialog(null, Localization.getMessage("SearchID"), "Error", JOptionPane.ERROR_MESSAGE);
            return daLoanType.getAll();
        }
    }

    public ArrayList<LoanType> getByName(String name) throws Exception {
        if (name.equals("")) {
            JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("NullError"), "Error", JOptionPane.ERROR_MESSAGE);
            return daLoanType.getAll();
        } else {
            return daLoanType.getByName(name);
        }
    }

    public boolean insert(String loanType, String interestR) throws Exception {
        int result = 0;
        String name = loanType;
        float interestRate = 0;
        boolean check = true;
        String error = "Error : \n"
                + "----------------------------------";
        if (name.equals("")) {
            check = false;
            error = error + "\n" + Localization.getInstance().getMessage("NullErrorLoanType");
        }
        try {
            interestRate = Float.parseFloat(interestR);
            if(interestRate <0){
                check = false;
                error = error + "\n" + "Interest Rate can not less than 0.";

            }
        } catch (Exception ex) {
            check = false;
            error = error + "\n" + Localization.getInstance().getMessage("InterestRateError");
            //ex.printStackTrace();
        }

        if (check) {
            result = daLoanType.insert(name, interestRate);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("InsertError"), "Error", JOptionPane.ERROR_MESSAGE);
                return false;
            }else {
                return true;
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }

    public boolean update(String loanTypeID, String typeOfLoan, String interestR) throws Exception{
        int result = 0;
        int id = 0;
        String name = typeOfLoan;
        float interestRate = 0;
        boolean check = true;
        String error = "Error : \n"
                + "----------------------------------";

        try {
            id = Integer.parseInt(loanTypeID);
        } catch (Exception e) {
            check = false;
            error = error + Localization.getInstance().getMessage("SearchID");
            //e.printStackTrace();
        }
        if (name.equals("")) {
            check = false;
            error = error + "\n" + Localization.getInstance().getMessage("NullErrorLoanType");
        }
        try {
            interestRate = Float.parseFloat(interestR);
            if(interestRate <0){
                check = false;
                error = error + "\n" + "Interest Rate can not less than 0.";

            }
        } catch (Exception ex) {
            check = false;
            error = error + "\n" + Localization.getInstance().getMessage("InterestRateError");
            //ex.printStackTrace();
        }


        if (check) {
            LoanType loanType = new LoanType(id, name, interestRate,true);
            result = daLoanType.update(loanType);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("UpdateError"), "Error", JOptionPane.ERROR_MESSAGE);
                return false;
            }else {
                return true;
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    
    public boolean disable(int loanTypeID) throws Exception
    {
        if(daLoanType.disable(loanTypeID) == 1){
            return true;
        }else{
            return false;
        }
    }

    public boolean enable(int loanTypeID) throws Exception
    {
        if(daLoanType.enable(loanTypeID) == 1){
            return true;
        }else{
            return false;
        }
    }
}
